# -*- coding: utf-8 -*-
# !/usr/bin/env python
from rest_framework.views import APIView
from components.kubernetes.api_file_verify import get_info
from utils.K8S import K8sApi
from utils.views import Result, ReturnClass


class UpdateImageVersion(APIView):
    def put(self, request):
        data = request.data
        old_kubernetes_cluster_id = data['old_kubernetes_cluster_id']
        old_version_cluster_select_value = data['old_version_cluster_select_value']
        data = data['data']

        now_return_info = get_info(old_kubernetes_cluster_id)
        if ReturnClass.if_ok(now_return_info):
            kubernetes_cluster_sign_kubeconfig = ReturnClass.get_value(now_return_info)
        else:
            return Result.fail_response(ReturnClass.get_value(now_return_info))

        now_k8s_api = K8sApi(kubernetes_cluster_sign_kubeconfig)

        return_data_one_list = []
        for data_one in data:
            return_data_one = now_k8s_api.replace_deployment_image(data_one['old_version_pod_name'],old_version_cluster_select_value,data_one['now_version_pod_image'])
            return_data_one_list.append(return_data_one)

        print(return_data_one_list)
        return Result.success_response(msg='提交成功')